package mess;

import java.util.Arrays;

public class LeeCode_16 {
    public static void main(String[] args) {
        int[] nums = {4,0,5,-5,3,3,0,-4,-5};
        System.out.println(threeSumClosest(nums, -2));
    }
    private static int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int threeSum;
        int distance;
        int min = nums[0] + nums[1] + nums[2] - target;
        min = min < 0 ? -min : min;
        int[] indexes = new int[3];
        indexes[1] = 1;
        indexes[2] = 2;
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                for (int k = j + 1; k < len; k++) {
                    threeSum = nums[i] + nums[j] + nums[k];
                    distance = target - threeSum;
                    distance = distance < 0 ? -distance : distance;
                    if (distance < min){
                        min = distance;
                        indexes[0] = i;
                        indexes[1] = j;
                        indexes[2] = k;
                    }
                }
            }
        }
        System.out.println(Arrays.toString(indexes));
        return nums[indexes[0]] + nums[indexes[1]] + nums[indexes[2]];
    }
}
